package com.vincce.sport.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.core.utils.ConvertUtil;
import com.vincce.sport.entity.*;
import com.vincce.sport.mapper.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.core.utils.imageUtil;

@Controller
@RequestMapping("/teamDate")
public class TeamDateController extends BaseController{
	
	@Resource
	private TeamDateMapper teamDateMapper;
	@Resource
	private TeamMapper teamMapper;
	
	@Resource
	private CompetitionSeasonMapper competitionSeasonMapper;
	@Resource
	private CityMapper cityMapper;
	@Resource
	private SchoolMapper schoolMapper;
	
	@Resource
	private ZoneMapper zoneMapper;
	@Resource
	private PlayerMapper playerMapper;
	@Resource
	private SchoolRankViewMapper schoolRankViewMapper;
	
	@Resource
	private TeamDataTaskMapper dataTaskMapper;
	
	@Resource
	private AdvertMapper advertMapper;
	@Resource
	private PlayerDateMapper playerDateMapper;

    @Resource
    private MatchTaskMapper matchTaskMapper;

    @Resource
    PreliminaryContestMapper preliminaryContestMapper;

    @Resource
    ShootSpotMapper shootSpotMapper;
	/**
	 * 球队数据(本赛季)
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/teamDateSeasonList")
	public String teamDateSeasonList(Model model,Integer id){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("teamId", id);
		
		List<TeamDate> teamDates = teamDateMapper.queryTeamDateSeason(map);
		for (int i = 0; i < teamDates.size(); i++) {
			float freeRate=teamDates.get(i).getFreeThrowHitRate()*100;
			teamDates.get(i).setFreeThrowHitRate(Float.parseFloat(String.format("%.1f", freeRate)));
			
			float twoRate=teamDates.get(i).getTwoPointsHitRate()*100;
			teamDates.get(i).setTwoPointsHitRate(Float.parseFloat(String.format("%.1f", twoRate)));
			
			float thrreRate=teamDates.get(i).getThreePointsHitRate()*100;
			teamDates.get(i).setThreePointsHitRate(Float.parseFloat(String.format("%.1f", thrreRate)));
			
			float totalHit=teamDates.get(i).getTotalHit()*100;
			teamDates.get(i).setTotalHit(Float.parseFloat(String.format("%.1f", totalHit)));

		}
		Team team =teamMapper.queryTeamDate(id);
		
		team.setLogUrl2(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+team.getLogUrl2(), "ori"));
		TeamDate pDate = teamDateMapper.querySeasonName(map);
		
		Page<Advert> footerAdvertPage=new Page<Advert>();
		Map<String, Object> filter = new HashMap<String, Object>();
		filter=new HashMap<String, Object>();	
	    filter.put("name", "球队本赛季数据");
	    filter.put("status", 1);
	    
	    footerAdvertPage.setLimit(1);
	    footerAdvertPage.setFilter(filter);
	    footerAdvertPage.setResult(replace1(advertMapper.queryAdverListPage(footerAdvertPage)));
	    model.addAttribute("footerAdvertPage",footerAdvertPage);
		/*for (int i = 0; i < teamDates.size(); i++) {
			//罚球命中率
			String freeThrowHitRate = teamDates.get(i).getFreeThrowHitRate()+"%";
			teamDates.get(i).setFreeThrowHitRate(freeThrowHitRate);
			
			String twoPointsHitRate=teamDates.get(i).getTwoPointsHitRate()+"%";
			teamDates.get(i).setTwoPointsHitRate(twoPointsHitRate);
			
			String threePointsHitRate=teamDates.get(i).getThreePointsHitRate()+"%";
			teamDates.get(i).setThreePointsHitRate(threePointsHitRate);
			
			
		}*/
		model.addAttribute("id",id);
		model.addAttribute("pDate",pDate);
		model.addAttribute("teamDates",teamDates);
		model.addAttribute("team",team);
		return "/foreground/teamDateSeason";
	}
	
	
	
	/**
	 * 球队数据(历史数据)
	 * @param model
	 * @param seasonId
	 * @param id
	 * @return
	 */
	@RequestMapping("/teamDateHistoryList")
	public String teamDateHistoryList(Model model,Integer seasonId,Integer id){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("teamId", id);
		map.put("seasonId", seasonId);
		List<TeamDate> teamDates = teamDateMapper.queryTeamDateHistory(map);
		for (int i = 0; i < teamDates.size(); i++) {
			float freeRate=teamDates.get(i).getFreeThrowHitRate()*100;
			teamDates.get(i).setFreeThrowHitRate(Float.parseFloat(String.format("%.1f", freeRate)));
			
			float twoRate=teamDates.get(i).getTwoPointsHitRate()*100;
			teamDates.get(i).setTwoPointsHitRate(Float.parseFloat(String.format("%.1f", twoRate)));
			
			float thrreRate=teamDates.get(i).getThreePointsHitRate()*100;
			teamDates.get(i).setThreePointsHitRate(Float.parseFloat(String.format("%.1f", thrreRate)));
			
			float totalHit=teamDates.get(i).getTotalHit()*100;
			teamDates.get(i).setTotalHit(Float.parseFloat(String.format("%.1f", totalHit)));

		}
		List<CompetitionSeason> competitionSeasonList=competitionSeasonMapper.querySeasonByAll();
		Team team =teamMapper.queryTeamDate(id);
		team.setLogUrl2(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+team.getLogUrl2(), "ori"));
		//TeamDate pDate = teamDateMapper.querySeasonName(map);
		//team.setLogUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+team.getLogUrl(), "ori"));
		TeamDate pDate = teamDateMapper.querySeasonName(map);
		
		Page<Advert> footerAdvertPage=new Page<Advert>();
		Map<String, Object> filter = new HashMap<String, Object>();
		filter=new HashMap<String, Object>();	
	    filter.put("name", "球队历史数据");
	    filter.put("status", 1);
	    
	    footerAdvertPage.setLimit(1);
	    footerAdvertPage.setFilter(filter);
	    footerAdvertPage.setResult(replace1(advertMapper.queryAdverListPage(footerAdvertPage)));
	    model.addAttribute("footerAdvertPage",footerAdvertPage);
		/*for (int i = 0; i < teamDates.size(); i++) {
			//罚球命中率
			String freeThrowHitRate = teamDates.get(i).getFreeThrowHitRate()+"%";
			teamDates.get(i).setFreeThrowHitRate(freeThrowHitRate);
			
			String twoPointsHitRate=teamDates.get(i).getTwoPointsHitRate()+"%";
			teamDates.get(i).setTwoPointsHitRate(twoPointsHitRate);
			
			String threePointsHitRate=teamDates.get(i).getThreePointsHitRate()+"%";
			teamDates.get(i).setThreePointsHitRate(threePointsHitRate);
			
			
		}*/
	    if (teamDates.size()>0) {
			model.addAttribute("pDate",teamDates.get(0).getSeasonName());
		}
		model.addAttribute("id",id);
		//model.addAttribute("pDate",pDate);
		model.addAttribute("teamDates",teamDates);
		model.addAttribute("team",team);
		model.addAttribute("competitionSeasonList",competitionSeasonList);
		return "/foreground/teamDateHistory";
		
	}
	
	@RequestMapping("/teamDataApi")
	@ResponseBody
	public String teamDataApi(Integer seasonId,String teamId){
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> result=new HashMap<String, Object>();
		if (seasonId==null || teamId==null) {
			result.put("suc", false);
			result.put("msg", "球队ID和赛季ID不能为空");
		}
		else{
			map.put("teamId", teamId);
			map.put("seasonId", seasonId);
			List<TeamDataApi> teamDates = teamDateMapper.queryTeamDateHistoryApi(map);
			result.put("list", teamDates);
			result.put("suc", true);
			result.put("msg", "成功");
		}
		
		return JSON.toJSONString(result);
	}
	
	/**
	 * 按照球队
	 * @param seasonId
	 * @param
	 * @return
	 */
	@RequestMapping("/teamDataByNameApi")
	@ResponseBody
	public String teamDataByNameApi(Integer seasonId,String teamName){
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> result=new HashMap<String, Object>();
		if (seasonId==null || teamName==null) {
			result.put("suc", false);
			result.put("msg", "球队名称和赛季ID不能为空");
		}
		else{
			map.put("teamName", teamName);
			map.put("seasonId", seasonId);
			List<TeamDataApi> teamDates = teamDateMapper.queryTeamDateHistoryApi(map);
			result.put("list", teamDates);
			result.put("suc", true);
			result.put("msg", "成功");
		}
		
		return JSON.toJSONString(result);
	}
	
	/**
	 * 球队数据API
	 * @param schoolName
	 * @param teamName
	 * @param contestId
	 * @return
	 */
	@RequestMapping("/queryTeamDataApi")
	@ResponseBody
	public String queryTeamDataApi(String schoolName,String teamName,Integer contestId){
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> result=new HashMap<String, Object>();
		if (schoolName==null || schoolName==null || contestId==null) {
			result.put("suc", false);
			result.put("msg", "学校名称球队名称和赛程ID不能为空");
		}
		
		else{
			map.put("schoolName", schoolName);
			map.put("teamName", teamName);
			map.put("contestId", contestId);
			
			List<TeamApi> apis=teamDateMapper.queryTeamApi(map);
			for (int i = 0; i < apis.size(); i++) {
				String score=apis.get(i).getTotalScore()+":"+apis.get(i).getTotalLossScore();
				apis.get(i).setScore(score);
			}
			result.put("list", apis);
			result.put("suc", true);
			result.put("msg", "成功");
		}
		
		return JSON.toJSONString(result);
	}
	
	
	/**
	 * 球队数据API  包括每一节的分数
	 * @param schoolName
	 * @param teamName
	 * @param contestId
	 * @return
	 */
	@RequestMapping("/queryTeamDataMatchesApi")
	@ResponseBody
	public String queryTeamDataMatchesApi(String schoolName,String teamName,Integer contestId){
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> result=new HashMap<String, Object>();
		if (schoolName==null || schoolName==null || contestId==null) {
			result.put("suc", false);
			result.put("msg", "学校名称球队名称和赛程ID不能为空");
		}
		
		else{
			map.put("schoolName", schoolName);
			map.put("teamName", teamName);
			map.put("contestId", contestId);
			
			List<TeamDataMatchesApi> apis=teamDateMapper.getTeamDataApi(map);
			for (int i = 0; i < apis.size(); i++) {
				String score=apis.get(i).getTotalScore()+":"+apis.get(i).getTotalLossScore();
				apis.get(i).setScore(score);
			}
			result.put("list", apis);
			result.put("suc", true);
			result.put("msg", "成功");
		}
		
		return JSON.toJSONString(result);
	}
	
	
	/**
	 * 根据学校名称，球队名称  查询当前赛季的最近一场比赛数据
	 * @param schoolName
	 * @param teamName
	 * @return
	 */
	@RequestMapping("/queryTeamByStatuApi")
	@ResponseBody
	public String queryTeamByStatuApi(String schoolName,String teamName){
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> result=new HashMap<String, Object>();
		if (schoolName==null || teamName==null) {
			result.put("suc", false);
			result.put("msg", "学校名称和球队名称不能为空");
		}
		
		else{
			map.put("schoolName", schoolName);
			map.put("teamName", teamName);
			
			List<TeamStatuApi> apis=teamDateMapper.queryTeamByStatuApi(map);
			for (int i = 0; i < apis.size(); i++) {
				String score=apis.get(i).getTotalScore()+":"+apis.get(i).getTotalLossScore();
				apis.get(i).setScore(score);
			}
			if (apis!=null && apis.size() > 0) {
				result.put("list", apis.get(0));
				result.put("msg", "成功");
			}else{
				result.put("msg", "暂无数据");
			}
			result.put("suc", true);
		}
		
		return JSON.toJSONString(result);
	}
	
	
	/**
	 * 根据学校名称球队名称 返回球队数据和球员数据URL
	 * @param schoolName
	 * @param teamName
	 * @return
	 */
	@RequestMapping("/queryTeamImgApi")
	@ResponseBody
	public String queryTeamImgApi(String schoolName,String teamName){
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> result=new HashMap<String, Object>();
		if (schoolName==null || teamName==null) {
			result.put("suc", false);
			result.put("msg", "学校名称和球队名称不能为空");
		}
		
		else{
			map.put("schoolName", schoolName);
			map.put("teamName", teamName);
			
			List<TeamImgApi> apis=teamDateMapper.queryTeamImgApi(map);
			if (apis!=null && apis.size() > 0) {
				result.put("list", apis);
				result.put("msg", "成功");
			}else{
				result.put("msg", "暂无数据");
			}
			result.put("suc", true);
		}
		
		return JSON.toJSONString(result);
	}
	
	public void createImage(TeamCreateImg teamCreateImg,TeamDate teamDate) throws IOException {
        List<InputStream> streams=imageUtil.stream(teamCreateImg);
        SimpleStorageClient.StorageResult obj = new SimpleStorageClient.StorageResult(200, "");
        SimpleStorageClient.StorageResult obj2 = new SimpleStorageClient.StorageResult(200, "");

        int av = (int) (Math.random()*1000000000);
        //球员数据url
        obj = SimpleStorageClient.upload(2, streams.get(0),av+"");
        //球队数据url
        obj2 = SimpleStorageClient.upload(2, streams.get(1),av+"");
        String teamPath=obj.info.replaceAll("\\\\", "/");
        String playerPath=obj2.info.replaceAll("\\\\", "/");
        teamDate.setPlayerDataUrl(playerPath);
        teamDate.setTeamDataUrl(teamPath);
        teamDateMapper.updateTeamDate(teamDate);
    }
	
	@RequestMapping("/teamDataCreateImgApi")
	@ResponseBody
	public String teamDataCreateImgApi(Integer matchId) throws IOException{
        //先拿到  matchId 的比赛对象
        Map<String, Object> result=new HashMap<String, Object>();
        Map<String, Object> map=new HashMap<String, Object>();

        PreliminaryContest preliminaryContest= preliminaryContestMapper.findPreliminaryContestById(Long.valueOf(matchId.toString()));
        if(CheckUtil.isEmpty(preliminaryContest)){
            result.put("suc", false);
            result.put("msg", "参数无效");
            return JSON.toJSONString(result);
        }else{
            map.put("contestId",matchId);
//            map.put("teamId",matchTaskEntity.getTeamId());//第一队
            List<TeamDate> teamDateList=teamDateMapper.queryTeamDateByContest(map);//赛季的两个球队数据
            TeamDate teamDateA=null;
            TeamDate teamDateB=null;
            List<PlayerDate> playListA=null;
            List<PlayerDate> playListB=null;
            School schoolA = null;
            School schoolB = null;
            if(teamDateList.size()<=0){
                result.put("suc", false);
                result.put("msg", "比赛未结束，没有数据！");
                return JSON.toJSONString(result);
            }
            for(int i=0;i<teamDateList.size();i++){
                if(teamDateList.get(i).getTeamId().toString().equals(preliminaryContest.getTeamId().toString()))
                    teamDateA=teamDateList.get(i);
                if(teamDateList.get(i).getTeamId().toString().equals(preliminaryContest.getTeamIdTwo().toString()))
                    teamDateB=teamDateList.get(i);
            }
            if(!CheckUtil.isEmpty(teamDateA)&&!CheckUtil.isEmpty(teamDateB)){
                map.put("teamId",teamDateA.getTeamId());
                playListA=playerDateMapper.queryPlayerDateByMachId(map);
                map.put("teamId",teamDateA.getTeamIdTwo());
                playListB=playerDateMapper.queryPlayerDateByMachId(map);
                schoolA= schoolMapper.queryByTeamId(teamDateA.getTeamId());
                schoolB= schoolMapper.queryByTeamId(teamDateB.getTeamId());
                map=new HashMap<String, Object>();
                map.put("contest_id",matchId);
                map.put("team_id",teamDateA.getTeamId());
                ShootSpot shootSpotA = shootSpotMapper.getshootSpotByTeamAndContest(map);
                map.put("team_id",teamDateB.getTeamId());
                ShootSpot shootSpotB = shootSpotMapper.getshootSpotByTeamAndContest(map);
                TeamCreateImg teamCreateImgA= ConvertUtil.createByDate(schoolA,schoolB,teamDateA,teamDateB,playListA);
                teamCreateImgA.setFullTimeUrl(shootSpotA.getFullTimeUrl());
                teamCreateImgA.setOpponentFullTimeUrl(shootSpotB.getFullTimeUrl());
                TeamCreateImg teamCreateImgB= ConvertUtil.createByDate(schoolB,schoolA,teamDateB,teamDateA,playListB);
                teamCreateImgB.setFullTimeUrl(shootSpotB.getFullTimeUrl());
                teamCreateImgB.setOpponentFullTimeUrl(shootSpotA.getFullTimeUrl());
                createImage(teamCreateImgA,teamDateA);
                createImage(teamCreateImgB,teamDateB);
                result.put("msg", "成功");
                return JSON.toJSONString(result);

            }else{
                result.put("suc", false);
                result.put("msg", "比赛数据有误");
                return JSON.toJSONString(result);
            }
        }
//
//
//		List<TeamCreateImg> teamCreateImgs=new ArrayList<TeamCreateImg>();
//		List<PlayerCreateImg> playerCreateImgs=new ArrayList<PlayerCreateImg>();
//		List<PlayerCreateImg> playerCreateImgs2=new ArrayList<PlayerCreateImg>();
//		TeamDate teamDate=new TeamDate();
//		TeamDate teamDate2=new TeamDate();
//		TeamCreateImg createImg=new TeamCreateImg();
//		TeamCreateImg createImg2=new TeamCreateImg();
//		if (matchId==null) {
//			result.put("suc", false);
//			result.put("msg", "matchId不能为空");
//		}
//		else{
//			map.put("matchId", matchId);
//			List<TeamCreateImg> teamDates = teamDateMapper.createImgTeamByPlayerApi(map);
//			/*for (int i = 0; i< teamDates.size(); i++) {
//				queryTeamDataCreateImg(matchId, teamDates.get(i).getTeamId());
//				map.put("teamId", teamDates.get(i).getTeamId());
//				teamCreateImgs = teamDateMapper.createImgTeamByPlayerApi(map);
//				playerCreateImgs=playerDateMapper.queryPlayerCreateImg(map);
//			}*/
//
//
//			if (teamDates!=null && teamDates.size() > 0) {
//
//				map.put("teamId", teamDates.get(0).getTeamId());
//				//teamCreateImgs = teamDateMapper.createImgTeamByPlayerApi(map);
//				createImg=teamDateMapper.createImgTeamByPlayerApiOne(map);
//				playerCreateImgs=playerDateMapper.queryPlayerCreateImg(map);
//				createImg.setList(playerCreateImgs);
//
//				List<InputStream> streams=imageUtil.stream(createImg);
//				SimpleStorageClient.StorageResult obj = new SimpleStorageClient.StorageResult(200, "");
//				SimpleStorageClient.StorageResult obj2 = new SimpleStorageClient.StorageResult(200, "");
//				SimpleStorageClient.StorageResult obj3 = new SimpleStorageClient.StorageResult(200, "");
//				SimpleStorageClient.StorageResult obj4 = new SimpleStorageClient.StorageResult(200, "");
//
//				int av = (int) (Math.random()*1000000000);
//				//球员数据url
//				obj = SimpleStorageClient.upload(2, streams.get(0),av+"");
//				//球队数据url
//				obj2 = SimpleStorageClient.upload(2, streams.get(1),av+"");
//				String teamPath=obj.info.replaceAll("\\\\", "/");
//				String playerPath=obj2.info.replaceAll("\\\\", "/");
//				teamDate.setId(teamDates.get(0).getId());
//				teamDate.setPlayerDataUrl(playerPath);
//				teamDate.setTeamDataUrl(teamPath);
//				teamDateMapper.updateTeamDate(teamDate);
//
//
//				map.clear();
//				map.put("matchId", matchId);
//				map.put("teamId", teamDates.get(1).getTeamId());
//				createImg2=teamDateMapper.createImgTeamByPlayerApiOne(map);
//				playerCreateImgs2=playerDateMapper.queryPlayerCreateImg(map);
//				createImg2.setList(playerCreateImgs2);
//				List<InputStream> streams2=imageUtil.stream(createImg2);
//				//球员数据url
//				obj3 = SimpleStorageClient.upload(2, streams2.get(0),av+"");
//				//球队数据url
//				obj4 = SimpleStorageClient.upload(2, streams2.get(1),av+"");
//				String playerPath2=obj3.info.replaceAll("\\\\", "/");
//				String teamPath2=obj4.info.replaceAll("\\\\", "/");
//				teamDate2.setId(teamDates.get(1).getId());
//				teamDate2.setPlayerDataUrl(playerPath2);
//				teamDate2.setTeamDataUrl(teamPath2);
//				teamDateMapper.updateTeamDate(teamDate2);
//
//				result.put("msg", "成功");
//			}
//			else{
//				result.put("msg", "暂无数据");
//			}
//			result.put("suc", true);
//
//		}
//
//		return JSON.toJSONString(result);
	}
	
	public static void main(String[] args) {
		System.out.println((int) (Math.random()*100000000));
	}
	public List<TeamCreateImg> queryTeamDataCreateImg(Integer matchId,Integer teamId){
		Map<String, Object> map=new HashMap<String, Object>();
		List<TeamCreateImg> teamDates = teamDateMapper.createImgTeamByPlayerApi(map);
		return teamDates;
	}
	
	/**
	 * 球队数据
	 * @param model
	 * @param schoolId
	 * @return
	 */
	@RequestMapping("/teamDataList")
	public String teamDataList(Model model,Integer schoolId,String zoneId,String cityId,String seasonId){
		List<CompetitionSeason> seasons= competitionSeasonMapper.querySeasonByAll();
		List<Zone> zones=zoneMapper.queryByZoneAll();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("schoolId", schoolId);
		map.put("zoneId", zoneId);
		map.put("cityId", cityId);
		map.put("seasonId", seasonId);
		List<TeamDataTask> teamDates = dataTaskMapper.queryTeamDataBySchoolId(map);
		/*for (int i = 0; i < teamDates.size(); i++) {
			float twoHit=teamDates.get(i).getTwoPointsHit()*100;
			teamDates.get(i).setTwoPointsHit(twoHit);
			
			
		}
		
		for (int i = 0; i < teamDates.size(); i++) {
			float threeHit=teamDates.get(i).getThreePointsHit()*100;
			teamDates.get(i).setThreePointsHit(threeHit);
		}*/
		model.addAttribute("zones",zones);
		model.addAttribute("seasons",seasons);
		model.addAttribute("teamDates",teamDates);
		
		model.addAttribute("schoolId",schoolId);
		model.addAttribute("zoneId",zoneId);
		model.addAttribute("cityId",cityId);
		model.addAttribute("seasonId",seasonId);
		return "/foreground/teamDataList";
		
		
	}
	
	
	@RequestMapping("/queryCityByZoneId")
	@ResponseBody
	public String queryCityByZoneId(Integer zoneId){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("zoneId", zoneId);
		List<City> list= cityMapper.queryCityByZoneId(map); 
		return JSON.toJSONString(list);
		
	}
	
	@RequestMapping("/querySchoolByCityId")
	@ResponseBody
	public String querySchoolByCityId(Integer cityId){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("cityId", cityId);
		List<School> schoolList=schoolMapper.queryByCityId(map);
		return JSON.toJSONString(schoolList);
	}
	
	/**
	 * 球队名册
	 * @param model
	 * @param teamId
	 * @return
	 */
	@RequestMapping("/queryTeamRoster")
	public String queryTeamRoster(Model model,Integer teamId,Long schoolId){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("teamId", teamId);
		map.put("status", 1);
		//查询球队下面的球员数据
		List<Player> playerList=playerMapper.queryPlayerByTeamId(map);
		replace(playerList);
		
		//查询除球员以为的其他角色
		List<Player> managerList= playerMapper.queryPlayerByStatus(map);
		replace(managerList);
		//所有球队
		List<Team> teamList=teamMapper.queryTeamByAll();
		List<Team> teamList2=teamMapper.queryTeamId(map);
		if (teamId!=null) {
			if (teamList2!=null) {
				model.addAttribute("teamName",teamList2.get(0).getName());
			}
		}
		else{
			model.addAttribute("teamName","");
		}
		SchoolRankView schoolRankView=new SchoolRankView();
		schoolRankView.setSchoolId(schoolId);
		
		Team team2=teamMapper.getTeamAndSchoolById(teamId.toString());
		
		//校内排名
		List<SchoolRankView> schoolRankViews = 
				schoolRankViewMapper.querySchoolDataByTeamId(schoolRankView);
		
		for (int i = 0; i < schoolRankViews.size(); i++) {
			if (schoolRankViews.get(i).getMatchLost()==null) {
				schoolRankViews.get(i).setMatchLost(0);
			}
			if (schoolRankViews.get(i).getMatchWon()==null) {
				schoolRankViews.get(i).setMatchWon(0);
			}
		}
		int i=1;
		for (SchoolRankView school: schoolRankViews) {
			school.setId(i);
			i++;
		}
		
		Team team = teamMapper.queryTeamById(teamId);
		replace(team);
		TeamDataTask dataTask = teamDateMapper.queryTeamDataTask(map);
		TeamDataTask dataTask2=dataTaskMapper.queryTeamAverageScoreRanking(map);
		TeamDataTask dataTask3=dataTaskMapper.queryTeamAverageBackboardRanking(map);
		TeamDataTask dataTask4=dataTaskMapper.queryTeamAverageAssistsAstsRanking(map);
		TeamDataTask dataTask5=dataTaskMapper.queryTeamAverageHitRateRanking(map);
		if (dataTask!=null) {
			float rate=dataTask.getAverageHitRate()*100;
			dataTask.setAverageHitRate(Float.parseFloat(String.format("%.1f", rate)));
		}
		
		if (dataTask==null) {
			dataTask=new TeamDataTask();
			
		}
		
		if (dataTask2==null) {
			dataTask2 = new TeamDataTask();
			dataTask2.setRownum(0);
		}
		if (dataTask3==null) {
			dataTask3 = new TeamDataTask();
			dataTask3.setRownum(0);
		}
		
		if (dataTask4 ==null) {
			dataTask4 = new TeamDataTask();
			dataTask4.setRownum(0);
		}
		
		if (dataTask5 ==null) {
			dataTask5 = new TeamDataTask();
			dataTask5.setRownum(0);
		}
		
		String seasonName="";
		List<CompetitionSeason> season =  competitionSeasonMapper.findCompetitionSeason(map);
		if (season.size()>0) {
			seasonName= season.get(0).getName();
		}
		
		
		Page<Advert> footerAdvertPage=new Page<Advert>();
		Map<String, Object> filter = new HashMap<String, Object>();
		filter=new HashMap<String, Object>();	
	    filter.put("name", "球队名册");
	    filter.put("status", 1);
	    
	    footerAdvertPage.setLimit(3);
	    footerAdvertPage.setFilter(filter);
	    footerAdvertPage.setResult(replace1(advertMapper.queryAdverListPage(footerAdvertPage)));
	    model.addAttribute("footerAdvertPage",footerAdvertPage);
	    
	    
	    
	    Page<Advert> footerAdvertPage1=new Page<Advert>();
		Map<String, Object> filter1 = new HashMap<String, Object>();
		filter1=new HashMap<String, Object>();	
	    filter1.put("name", "球队名册底部广告");
	    filter1.put("status", 1);
	    
	    footerAdvertPage1.setLimit(1);
	    footerAdvertPage1.setFilter(filter1);
	    footerAdvertPage1.setResult(replace1(advertMapper.queryAdverListPage(footerAdvertPage1)));
	    model.addAttribute("footerAdvertPage1",footerAdvertPage1);
	    
	    
	    
	    
		model.addAttribute("seasonName",seasonName);
		model.addAttribute("dataTask",dataTask);
		model.addAttribute("dataTask2",dataTask2);
		model.addAttribute("dataTask3",dataTask3);
		model.addAttribute("dataTask4",dataTask4);
		model.addAttribute("dataTask5",dataTask5);
		model.addAttribute("team",team);
		model.addAttribute("team2",team2.getSchoolId());
		model.addAttribute("schoolRankViews",schoolRankViews);
		model.addAttribute("teamList",teamList);
		model.addAttribute("playerList",playerList);
		model.addAttribute("managerList",managerList);
		model.addAttribute("schoolId",schoolId);
		model.addAttribute("teamId",teamId);
		//model.addAttribute("teamName",teamName);
		return "/foreground/teamRoster";
		
	}
	
	/**
	 * 球队成绩
	 * @param model
	 * @param teamId
	 * @param schoolId
	 * @return
	 */
	@RequestMapping("/queryTeamScore")
	public String queryTeamScore(Model model,Integer teamId,Long schoolId){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("teamId", teamId);
		map.put("status", 1);
		Team team = teamMapper.queryTeamById(teamId);
		replace(team);
		Team team2=teamMapper.getTeamAndSchoolById(teamId.toString());
		List<TeamDate> teamDates = teamDateMapper.queryTeamDateSeason(map);
		for (int i = 0; i < teamDates.size(); i++) {
			float hitRate=teamDates.get(i).getShootBasketHitRate()*100;
			teamDates.get(i).setShootBasketHitRate(hitRate);
		}
		//所有球队
	    List<Team> teamList=teamMapper.queryTeamByAll();
	    
	    List<Team> teamList2=teamMapper.queryTeamId(map);
		if (teamId!=null) {
			if (teamList2!=null) {
				model.addAttribute("teamName",teamList2.get(0).getName());
			}
		}
		else{
			model.addAttribute("teamName","");
		}
		//校内排名
		SchoolRankView schoolRankView=new SchoolRankView();
		schoolRankView.setSchoolId(schoolId);
		
		List<SchoolRankView> schoolRankViews = 
				schoolRankViewMapper.querySchoolDataByTeamId(schoolRankView);
		for (int i = 0; i < schoolRankViews.size(); i++) {
			if (schoolRankViews.get(i).getMatchLost()==null) {
				schoolRankViews.get(i).setMatchLost(0);
			}
			if (schoolRankViews.get(i).getMatchWon()==null) {
				schoolRankViews.get(i).setMatchWon(0);
			}
			
			
		}
		int i=1;
		for (SchoolRankView school: schoolRankViews) {
			school.setId(i);
			i++;
		}
		
		TeamDataTask dataTask = teamDateMapper.queryTeamDataTask(map);
		TeamDataTask dataTask2=dataTaskMapper.queryTeamAverageScoreRanking(map);
		TeamDataTask dataTask3=dataTaskMapper.queryTeamAverageBackboardRanking(map);
		TeamDataTask dataTask4=dataTaskMapper.queryTeamAverageAssistsAstsRanking(map);
		TeamDataTask dataTask5=dataTaskMapper.queryTeamAverageHitRateRanking(map);
		if (dataTask!=null) {
			float rate=dataTask.getAverageHitRate()*100;
			dataTask.setAverageHitRate(Float.parseFloat(String.format("%.1f", rate)));
		}
		
		if (dataTask==null) {
			dataTask=new TeamDataTask();
			
		}
		
		if (dataTask2==null) {
			dataTask2 = new TeamDataTask();
			dataTask2.setRownum(0);
		}
		if (dataTask3==null) {
			dataTask3 = new TeamDataTask();
			dataTask3.setRownum(0);
		}
		
		if (dataTask4 ==null) {
			dataTask4 = new TeamDataTask();
			dataTask4.setRownum(0);
		}
		
		if (dataTask5 ==null) {
			dataTask5 = new TeamDataTask();
			dataTask5.setRownum(0);
		}
		
		Page<Advert> footerAdvertPage=new Page<Advert>();
		Map<String, Object> filter = new HashMap<String, Object>();
		filter=new HashMap<String, Object>();	
	    filter.put("name", "球队成绩");
	    filter.put("status", 1);
	    
	    footerAdvertPage.setLimit(3);
	    footerAdvertPage.setFilter(filter);
	    footerAdvertPage.setResult(replace1(advertMapper.queryAdverListPage(footerAdvertPage)));
	    model.addAttribute("footerAdvertPage",footerAdvertPage);
	    
	    
	    String seasonName="";
		List<CompetitionSeason> season =  competitionSeasonMapper.findCompetitionSeason(map);
		if (season.size()>0) {
			seasonName= season.get(0).getName();
		}
		
		model.addAttribute("seasonName",seasonName);
		model.addAttribute("dataTask",dataTask);
		model.addAttribute("dataTask2",dataTask2);
		model.addAttribute("dataTask3",dataTask3);
		model.addAttribute("dataTask4",dataTask4);
		model.addAttribute("dataTask5",dataTask5);
		model.addAttribute("team",team);
		model.addAttribute("team2",team2.getSchoolId());
		model.addAttribute("schoolRankViews",schoolRankViews);
		model.addAttribute("schoolId",schoolId);
		model.addAttribute("teamDates",teamDates);
		model.addAttribute("teamId",teamId);
		model.addAttribute("teamList",teamList);
		return "/foreground/teamDataScore";
	}
	
	
	
	
	/**
	 * 图片路径替换
	 * @param
	 * @return
	 */
	public List<Player> replace(List<Player> imgTempList){
		if(imgTempList!=null && imgTempList.size()>0){
			for(Player itl:imgTempList){
				itl.setUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getUrl(), "ori"));
			}
		}
		return imgTempList;
	}
	
	public Team replace(Team team){
		if (team!=null) {
			team.setLogUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+team.getLogUrl(), "ori"));
		}
		
		return team;
	}
	
	
	/**
	 * 图片路径替换
	 * @param
	 * @return
	 */
	public List<Advert> replace1(List<Advert> advertList){
		if(advertList!=null && advertList.size()>0){
			for(Advert adv:advertList){
				adv.setAdvertImg(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+adv.getAdvertImg(), "ori"));
			}
		}
		return advertList;
	}
	
	
	
	
}
